const express = require("express");
const router = express.Router();
const { conMysql } = require("../db");

//导入r2-custom-id 生成8位id
const customId = require('r2-custom-id');



// 添加企业信息
router.post("/addEnterprise", (req, res) => {

    let { enterprise_name, enterprise_desc, tax_no, contact_person, contact_number, enterprise_introduction = '' } = req.body;
    //判断是否为空
    if (!enterprise_name || !enterprise_desc || !tax_no || !contact_person || !contact_number) {
        res.output("添加失败,信息不能为空", 500, null);
        return;
    }

    //使用 customId 随机生成10位id 
    let enterprise_id = customId({
        length: 10,
        uniqueId: 8848
    });
    const sql1 = `select * from db_enterprise where  enterprise_name='${enterprise_name}' or tax_no='${tax_no}'`;
    conMysql(sql1).then((result) => {
        if (result.length > 0) {
            res.output("企业信息已存在、请检查企业名称或统一社会信用代码", 500, null);
            return;
        }
        // 转义HTML内容
        //const escapedContent = mysql.escape(enterprise_introduction);

        //处理转义后的内容 添加\\ 

        const sql = `INSERT INTO db_enterprise (enterprise_id,enterprise_desc,enterprise_name,tax_no,contact_person,contact_number,enterprise_introduction) VALUES ('${enterprise_id}','${enterprise_desc}','${enterprise_name}','${tax_no}','${contact_person}','${contact_number}','${JSON.stringify(enterprise_introduction)}')`;
        conMysql(sql)
            .then((result) => {

                res.output("添加成功", 0, null);
            })
            .catch((err) => {



                res.output(err, 500, null);
            });


    }).catch((err) => {
        res.output(err, 500, null);
    });


});

module.exports = router;